

package com.kimo.shop.user.api.controller;

import cn.hutool.core.lang.Snowflake;
import com.kimo.shop.bean.pay.PayInfoDto;
import com.kimo.shop.common.enums.PayType;
import com.kimo.shop.common.exception.KimoShopBindException;
import com.kimo.shop.security.api.model.TaurusUser;
import com.kimo.shop.security.api.util.SecurityUtils;
import com.kimo.shop.service.PayManagerService;
import com.kimo.shop.user.comment.model.UserBalance;
import com.kimo.shop.user.comment.model.UserBalanceLog;
import com.kimo.shop.user.comment.param.PayUserParam;
import com.kimo.shop.user.comment.service.UserBalanceLogService;
import com.kimo.shop.user.comment.service.UserBalanceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.Objects;

/**
 * @author kimo-taozhouchuan on 2020/03/02.
 */
@Api(tags = "余额支付接口")
@RestController
@RequestMapping("/p/balance")
@AllArgsConstructor
public class PayBalanceController {

    /**
     * 会员信息
     */
    private final UserBalanceService userBalanceService;
    private final UserBalanceLogService userBalanceLogService;
    private final Snowflake snowflake;
    private final PayManagerService payManagerService;

    /**
     * 会员余额充值接口
     */
    @PostMapping("/pay")
    @ApiOperation(value = "会员余额充值", notes = "会员余额充值")
    @SneakyThrows
    public ResponseEntity<?> payVip(HttpServletResponse httpResponse, @RequestBody PayUserParam payCardParam) {
        TaurusUser user = SecurityUtils.getUser();
        String openId = user.getBizUserId();
        if (!user.isEnabled()) {
            // 您已被禁用，不能购买，请联系平台客服
            throw new KimoShopBindException("kimo.order.pay.user.disable");
        }
        if (payCardParam.getPayType().equals(PayType.SCOREPAY.value())){
            //余额充值，不能使用积分支付
            throw new KimoShopBindException("kimo.user.payType.error");
        }else if (payCardParam.getPayType().equals(PayType.SCOREPAY.value())){
            //余额充值，不能使用余额支付
            throw new KimoShopBindException("kimo.user.payType.error");
        }
        // 使用雪花算法生成支付单号
        String payNo = String.valueOf(snowflake.nextId());
        Date now = new Date();
        UserBalance userBalance = userBalanceService.getById(payCardParam.getId());
        if (Objects.isNull(userBalance)) {
            // 当前余额充值选项已被删除
            throw new KimoShopBindException("kimo.user.blance.not.exist");
        }

        //生成余额充值日志
        UserBalanceLog userBalanceLog = new UserBalanceLog();
        userBalanceLog.setCreateTime(now);
        userBalanceLog.setIoType(1);
        userBalanceLog.setType(1);
        userBalanceLog.setIsPayed(0);
        userBalanceLog.setPayNo(payNo);
        userBalanceLog.setBalanceId(userBalance.getBalanceId());
        userBalanceLog.setUserId(user.getUserId());
        userBalanceLog.setChangeBalance(userBalance.getRechargeAmount());
        userBalanceLogService.save(userBalanceLog);
        PayInfoDto payInfo = new PayInfoDto();
        payInfo.setBody("余额充值购买");
        payInfo.setPayNo(payNo);
        payInfo.setPayAmount(userBalance.getRechargeAmount());
        payInfo.setBizUserId(openId);
        payInfo.setPayType(payCardParam.getPayType());
        payInfo.setApiNoticeUrl("/notice/balancePay/payCallback/" + payCardParam.getPayType());
        payInfo.setReturnUrl(payCardParam.getReturnUrl());
//        payManagerService.doPay(httpResponse, payInfo);
//        userBalanceService.paySuccess(payInfo.getPayNo());
//        return null;
        return payManagerService.doPay(httpResponse, payInfo);
    }
}
